﻿using spaar.ModLoader;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
using UnityEngine.SceneManagement;

namespace XultimateX.ScalingBlockMod
{
    class ScalingBlock : SingleInstance<ScalingBlock>
    {

        public override string Name { get; } = "Scaling Block Script";

        internal static int ScalingBlockNum = 0;

        internal static bool start = false;

        internal static List<BlockBehaviour> ScalingBlocks = new List<BlockBehaviour>();

        private void Start()
        {
            SceneManager.sceneLoaded += ActivateHiddenBlock;

            //InitScalingBlockSliders();
            //加载配置
            XmlLoader.OnLoad += LoadConfiguration;

            Game.OnBlockPlaced += AddSliders;
            Game.OnKeymapperOpen += () =>
            {
                if (!HasSliders(BlockMapper.CurrentInstance.Block))
                    AddSliders(BlockMapper.CurrentInstance.Block);
                AddAllSliders();
            };
            
            
        }

        void LoadConfiguration(MachineInfo info)
        {
            foreach (var blockinfo in info.Blocks)
            {
                if (blockinfo.ID == (int)BlockType.ScalingBlock)
                {
                    
                }

            }

            foreach (var block in Machine.Active().BuildingBlocks.FindAll(block => block.GetBlockID() == (int)BlockType.ScalingBlock))
            {
                Debug.Log("有蒙皮模块");
                
            }
        }

        private static void ActivateHiddenBlock(Scene s, LoadSceneMode mode)
        {
            var b = GameObject.Find("HUD/BottomBar/AlignBottomLeft/BLOCK BUTTONS/t_BLOCKS/Scaling Block");
            b?.SetActive(true);

            Debug.Log("sceneLoaded");
        }

        private void Update()
        {
            if (Input.GetKeyDown(KeyCode.Q))
            {
                foreach (var block in Machine.Active().BuildingBlocks.FindAll(block => block.GetBlockID() == (int)BlockType.ScalingBlock))
                {
                    Debug.Log("有蒙皮脚本? " + GetComponent<ScalingBlockScript>() != null);

                }
            }

                if (Game.AddPiece)
            {
                if (!start)
                {
                    start = true;
                    Debug.Log("Game.AddPiece");
                    // FindAllScalingBlock();
                    Debug.Log(GameObject.Find("New Game Object"));
                }

            }
            else
            {
                start = false;
            }

            if (StatMaster.isSimulating)
            {
                //if (!start)
                //{
                //    start = true;
                //    StartSimulate();
                //}
                //else
                //{
                //    UpdateSimulate();
                //}
                
            }
            else
            {
                //start = false;
            }
        }

        public struct ScalingBlockData
        {
            public Guid guid;
            public float RotationModel;
        }

        public static void InitScalingBlockSliders(BlockBehaviour BB)
        {
            var currentMapperTypes = BB.MapperTypes;

            var SBD = new ScalingBlockData() { RotationModel = 0 };
            var RotationModelSlider = new MSlider("旋转模型", "RotationModel", SBD.RotationModel, -180f, 180f);
            RotationModelSlider.ValueChanged += (float value) => { SBD.RotationModel = value; };
            currentMapperTypes.Add(RotationModelSlider);
            ScalingBlocks.Add(BB);
            BB.gameObject.AddComponent<ScalingBlockScript>();

            ScalingBlockMod.mapperTypesField.SetValue(BB, currentMapperTypes);
        }

        public static bool HasSliders(BlockBehaviour block)
        {
            return  block.GetBlockID() == (int)BlockType.ScalingBlock && block.MapperTypes.Exists(match => match.Key == "RotationModel");
        }

        public static void AddAllSliders()
        {
            foreach (BlockBehaviour block in Machine.Active().BuildingBlocks.FindAll(block => !HasSliders(block)))
            {
                AddSliders(block);
            }
        }

        public static void AddSliders(Transform block)
        {
            BlockBehaviour blockbehaviour = block.GetComponent<BlockBehaviour>();
            if (!HasSliders(blockbehaviour))
                AddSliders(blockbehaviour);

        }

        private static void AddSliders(BlockBehaviour block)
        {
            if (block.GetBlockID() == (int)BlockType.ScalingBlock)
            {
                Debug.Log("添加组件");

                InitScalingBlockSliders(block);

            }

        }


        private static void FindAllScalingBlock()
        {

            foreach (var block in Machine.Active().BuildingBlocks.FindAll(block => block.GetBlockID() == (int)BlockType.ScalingBlock))
            {
                FindScalingBlock(block);
            }
                
        }

        private static bool FindScalingBlock(BlockBehaviour block)
        {
                ScalingBlockNum++;
            //Debug.Log(block.GetComponent<ScalingBlockScript>() != null && block.GetBlockID() == (int)BlockType.ScalingBlock ? true : false);
            return block.GetComponent<ScalingBlockScript>() != null && block.GetBlockID() == (int)BlockType.ScalingBlock ? true : false;
        }

        internal protected virtual void StartSimulate() { Debug.Log("start "); FindAllScalingBlock(); AddAllScript(); }

        internal protected virtual void UpdateSimulate() { }

        private  void AddAllScript()
        {
            foreach (var block in Machine.Active().BuildingBlocks.FindAll(block => block.GetBlockID() == (int)BlockType.ScalingBlock))
            {
                AddScript(block);
            }
        }

        private  void AddScript(BlockBehaviour block)
        {
            if(block.GetComponent<ScalingBlockScript>() == null)
            block.gameObject.AddComponent<ScalingBlockScript>();
            Debug.Log("addscript");
        }

        private static bool HasScript(BlockBehaviour block)
        {
            return block.GetComponent<ScalingBlockScript>() != null && block.GetBlockID() == (int)BlockType.ScalingBlock ? true : false;
        }



        class ScalingBlockScript : MonoBehaviour
        {
            private void Start()
            {
                Debug.Log("sbs");
            }
        }
    }
}
